package leecoke;

public class Test083 {
    public static class ListNode{
        int val;
        ListNode next;

        ListNode(int val) {
            this.val = val;
        }
    }

    public static ListNode deleteDuplicates(ListNode head) {
        // 没有节点或者只有一个节点则直接返回
        if(head == null || head.next == null){
            return head;
        }
        ListNode cur = head;
        ListNode temp = null;
        while (cur != null && cur.next != null) {
            temp = cur.next;
            if (cur.next != null && cur.val == temp.val) {
                cur.next = temp.next;
            } else {
                cur = temp;
            }
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(1);
        head.next.next = new ListNode(1);
        head.next.next.next = new ListNode(1);
        head.next.next.next.next = new ListNode(1);
        printNode(head);
        deleteDuplicates(head);
        printNode(head);
    }


    public static void printNode(ListNode node) {
        while (node != null) {
            System.out.print(node.val + " ");
            node = node.next;
        }
        System.out.println();
    }
}
